#include "udp_utils.hpp"
#include <thread>
#include <chrono>

void udp_sender(const std::string& ip, uint16_t port, bool routed = false) {
    socket_t fd = create_udp_socket();
    if (fd == INVALID_SOCKET_VALUE) { std::cerr << "Create socket failed\n"; return; }

    auto serverAddr = make_sockaddr(ip, port);

    std::string msgstr = routed ? "hello, agent route!" : "hello, direct!";
    std::vector<uint8_t> payload(msgstr.begin(), msgstr.end());
    Message msg(SGW_HEARTBEAT, payload, 1, routed);
    if (routed) {
        msg.routers[0] = Address(inet_addr("192.168.1.1"), htons(8080));
        msg.routers[1] = Address(inet_addr("192.168.1.2"), htons(8081));
        // 其余路由可按需赋值
    }
    auto buf = msg.pack();
    ssize_t sent = sendto(fd, buf.data(), buf.size(), 0, reinterpret_cast<const sockaddr*>(&serverAddr), sizeof(serverAddr));
    std::cout << "Send: " << sent << " bytes, routed=" << routed << std::endl;
    close(fd);
}